package cc.w3d.jawos.apps.artiks.client.rpc;

import java.util.List;

import cc.w3d.apps.cats.catsManager.client.beans.Cat;
import cc.w3d.apps.cats.catsManager.client.rpc.CatsManagerService;
import cc.w3d.apps.cats.catsManager.client.rpc.CatsManagerServiceAsync;
import cc.w3d.jawos.apps.artiks.manager.client.beans.Article;
import cc.w3d.jawos.apps.artiks.manager.client.beans.ResourceItem;
import cc.w3d.jawos.apps.artiks.manager.client.beans.ResourceType;
import cc.w3d.jawos.apps.artiks.manager.client.rpc.ArtiksManagerService;
import cc.w3d.jawos.apps.artiks.manager.client.rpc.ArtiksManagerServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ArtiksService
{
	/* <Service> */
	private ArtiksManagerServiceAsync service = null;
	private CatsManagerServiceAsync catsService = null;

	public void initService()
	{
		configureProcessingPopup();
		configureErrorReport();
		service     = (ArtiksManagerServiceAsync) GWT.create(ArtiksManagerService.class);
		catsService = (CatsManagerServiceAsync)   GWT.create(CatsManagerService.class);
	}
	/* </Service> */

	/* <ProcessingPopup> */
	private static PopupPanel processingPopup = null;

	private static void configureProcessingPopup()
	{
		if(processingPopup == null)
		{
			processingPopup = new PopupPanel(false, true);
			HTML processingPopupContent = new HTML("<center><i>Processing...</i></center>");
			processingPopupContent.setPixelSize(80, 20);

			processingPopup.setGlassEnabled (true);
			processingPopup.add             (processingPopupContent);
		}
	}

	static void showProcessingPopup() {processingPopup.center();}
	static void hideProcessingPopup() {processingPopup.hide();}
	/* </ProcessingPopup> */

	/* <ErrorReport> */
	private static DialogBox errorReport = null;
	private static HTML   errorInfo   = new HTML();

	private static void configureErrorReport()
	{
		if(errorReport == null)
		{
			errorReport = new DialogBox(false, true);
			VerticalPanel main = new VerticalPanel();

			errorInfo   = new HTML();

			main.add(errorInfo);
			errorReport.setGlassEnabled(true);
			errorReport.setWidth("640px");
			errorReport.setHeight("480px");

			Button ok = new Button("Aceptar", new ClickHandler()
				{public void onClick(ClickEvent arg0) {errorReport.hide();}});

			main.add(ok);
			main.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);

			errorReport.setWidget(main);
		}
	}

	static void reportError(String error)
	{
		errorInfo.setHTML(error);
		errorReport.center();
	}
	/* </ErrorReport> */

	public void addChild(String parentResourceId, ResourceType type, String name, ArtiksAsyncCallback<ResourceItem> callback)
		{service.addChild(parentResourceId, type, name, callback);}

	public void getChilds(String id, ArtiksAsyncCallback<List<ResourceItem>> callback)
		{service.getChilds(id, callback);}

	public void removeItem(String itemId, ArtiksAsyncCallback<Void> callback)
		{service.removeItem(itemId, callback);}


	public void getArticle(String itemId, ArtiksAsyncCallback<Article> callback)
		{service.getArticle(itemId, callback);}

	public void setArticle(String itemId, Article article, ArtiksAsyncCallback<Void> callback)
		{service.setArticle(itemId, article, callback);}

	public void getCats(ArtiksAsyncCallback<List<Cat>> callback)
		{catsService.getCats(callback);}
}
